<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="doClose"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="账号" prop="username">
        <el-input
          v-model="dataForm.username"
          placeholder="账号"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="工号/学号" prop="personalNumber">
        <el-input
          v-model="dataForm.personalNumber"
          placeholder="工号/学号"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="name">
        <el-input
          v-model="dataForm.name"
          placeholder="真实姓名"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="dataForm.email"
          placeholder="邮箱"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="mobile">
        <el-input
          v-model="dataForm.mobile"
          placeholder="电话号码"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属部门" prop="departmentId">
        <department-select-tree
          @input="getID"
          ref="SelectTree"
          v-model="dataForm.departmentId"
          class="inputClass"
        />
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status" class="inputClass">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input
          type="textarea"
          v-model="dataForm.memo"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input
          v-model="dataForm.createTime"
          placeholder="创建时间"
          disabled
          class="inputClass"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="doClose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isMobile } from '@/utils/validate'
import DepartmentSelectTree from './department-select-tree.vue'
export default {
  components: { DepartmentSelectTree },
  data() {
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        username: '',
        name: '',
        email: '',
        mobile: '',
        departmentId: '',
        departmentName: '',
        status: 1,
        memo: '',
        createTime: '',
        personalNumber: null
      },
      dataRule: {
        username: [{ required: true, message: '账号', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        mobile: [
          { required: true, message: '电话号码不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/select'),
        method: 'get',
        params: this.$http.adornParams()
      })
        .then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        })
        .then(() => {
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(
                `sys/visitor/getVisitorById/${this.dataForm.userId}`
              ),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 1) {
                this.dataForm = data.data
                this.$nextTick(() => {
                  this.$refs.SelectTree.getDepartments()
                })
              }
            })
          }
        })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((vaild) => {
        if (vaild) {
          this.$http({
            url: this.$http.adornUrl(
              `sys/visitor/${
                !this.dataForm.userId ? 'addVisitor' : 'updateVisitor'
              }`
            ),
            method: `${!this.dataForm.userId ? 'post' : 'put'}`,
            data: this.$http.adornData({
              ...this.dataForm,
              type: 5,
              createUserId: this.$store.state.user.id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$refs['dataForm'].resetFields()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // getID
    getID(id) {
      this.dataForm.departmentId = id
    },
    doClose() {
      this.visible = false
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.inputClass {
  width: 400px;
}
</style>